﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace BoilingWater
{
    public partial class MainForm : Form
    {
        private PortControlHelper pchReceive;
        private int sec = 0;
        const string SeriesName = "水的沸腾";

        public MainForm()
        {
            InitializeComponent();
            pchReceive = new PortControlHelper();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "时间/min";
            ch.Width = 180;
            lv_table.Columns.Add(ch);

            for(double i = 0; i<18; i += 0.5)
            {
                ColumnHeader c = new ColumnHeader();
                c.Text = i.ToString();
                c.Width = 80;
                lv_table.Columns.Add(c);
            }

            ListViewItem item = new ListViewItem();
            //序号
            item.Text = "温度/℃";
            for(int i=0; i<35; i++)
                item.SubItems.Add("");

            lv_table.BeginUpdate();
            lv_table.Items.Add(item);
            lv_table.EndUpdate();

            cb_portName.Items.AddRange(pchReceive.PortNameArr);
            if (cb_portName.Items.Count > 0)
            {
                cb_portName.SelectedIndex = 0;
            }


            initchart();
        }

        void initchart()
        {
            var chart = chart1.ChartAreas[0];
            chart.AxisX.IntervalType = DateTimeIntervalType.Number;

            chart.AxisX.ArrowStyle = AxisArrowStyle.Lines;
            chart.AxisY.ArrowStyle = AxisArrowStyle.Lines;

            chart.AxisX.Title = "时间/min";
            chart.AxisY.Title = "温度/℃";

            chart.AxisX.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.IsEndLabelVisible = true;

            chart.AxisX.Minimum = 0;
            chart.AxisX.Maximum = 15;
            chart.AxisY.Minimum = 50;
            chart.AxisY.Maximum = 110;
            chart.AxisX.Interval = 1;
            chart.AxisY.Interval = 10;

            chart1.Series.Add(SeriesName);

            //绘制折线图
            chart1.Series[SeriesName].ChartType = SeriesChartType.Spline;

            chart1.Series[SeriesName].Color = Color.Red;
            chart1.Series[0].IsVisibleInLegend = false;

        }

        /// <summary>
        /// 接收到的数据，写入文本框内
        /// </summary>
        /// <param name="data"></param>
        private void ComReceiveData(string data)
        {
            this.Invoke(new EventHandler(delegate
            {
                double wd = double.Parse(data);
                //wd = wd - 3.5;

                //if (wd > 98.5) wd = 98.5;

                System.Console.WriteLine($"接收：{data}");

                if (sec % 30 == 0)
                {
                    ListViewItem item = lv_table.Items[0];
                    item.SubItems[sec/30+1].Text = wd.ToString();
                    lv_table.Update();
                }

                double sj = ((double)sec) / 60.0;

                lb_time.Text = string.Format("时间{0:d2}:{1:d2}", sec / 60, sec % 60);

                chart1.Series[SeriesName].Points.AddXY(sj, wd);

                sec++;

            }));
        }

        private void bt_open_Click(object sender, EventArgs e)
        {
            string portName = (string)cb_portName.SelectedItem;
            int baudRate = 9600;
            int dataBit = 8;
            int stopaBit = 3;
            int timeout = 1000;

            pchReceive.OpenPort(
                portName,
                baudRate,
                dataBit,
                stopaBit,
                timeout);

            pchReceive.OnComReceiveDataHandler += new PortControlHelper.ComReceiveDataHandler(ComReceiveData);
            
            sec = 0;
            timer1.Start();
        }


        private void bt_close_Click(object sender, EventArgs e)
        {
            close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pchReceive.SendData("1");
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (timer1.Enabled)
            {
                close();
            }
        }


        private void close()
        {
            timer1.Stop();
            pchReceive.OnComReceiveDataHandler -= new PortControlHelper.ComReceiveDataHandler(ComReceiveData);
            pchReceive.ClosePort();
        }

        private void bt_camera_Click(object sender, EventArgs e)
        {
            new CameraForm().ShowDialog();
        }

    }
}
